package I_Q.hvpcs.boot.vpc.vision;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import I_Q.hvpcs.cache.DetaCacheManager;
import I_Q.hvpcs.boot.sets.StableBlob;
import I_Q.hvpcs.boot.sets.VPCSRequest;
import I_Q.hvpcs.boot.sets.VPCSResponse;

/*
 * 个人著作权人, 作者 罗瑶光, 浏阳
 * yaoguangluo@outlook.com, 313699483@qq.com, 2080315360@qq.com,
 * (lyg.tin@gmail.com2018年回国后因国内G网屏蔽不再使用）
 * 15116110525-浏阳德塔软件开发有限公司-创始人-法人-总经理-永久非盈利-
 * 430181198505250014, G24402609, EB0581342
 * 204925063, 389418686, F2406501, 0626136
 * 湖南省 浏阳市 集里街道 神仙坳社区 大塘冲一段路 208号 阳光家园别墅小区 第十栋别墅 第三层
 * */
public class RestMapVision_XP_buffer extends RestMapVision {

	public static void processBuffer(VPCSRequest vPCSRequest,
			VPCSResponse vPCSResponse) {
		try {
			if (null == vPCSRequest.getRequestFilePath()
					|| null == vPCSRequest.getRequestLink()) {
				return;
			}

			String builderToString;
			if (DetaCacheManager.getCacheOfString(
					vPCSRequest.getRequestFilePath()) != null) {
				builderToString = DetaCacheManager
						.getCacheOfString(vPCSRequest.getRequestFilePath());
			} else {
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.append("HTTP/1.1 200 OK").append("\n");
				stringBuilder.append("Host:deta software  \n");
				stringBuilder.append("Cache-control: max-age=315360000 \n");
				stringBuilder.append(vPCSResponse.getResponseContentType());
				File file = new File(vPCSRequest.getRequestFilePath());
				vPCSResponse.fileInputStream = new FileInputStream(file);
				vPCSResponse.inputStreamReader = new InputStreamReader(
						vPCSResponse.fileInputStream,
						vPCSRequest.getRequestFileCode());
				vPCSResponse.bufferedReader = new BufferedReader(
						vPCSResponse.inputStreamReader);
				String line = null;
				while ((line = vPCSResponse.bufferedReader
						.readLine()) != null) {
					stringBuilder.append(line);
				}
				builderToString = stringBuilder.toString();
				DetaCacheManager.putCacheOfString(
						vPCSRequest.getRequestFilePath(), builderToString);
				vPCSResponse.bufferedReader.close();
			}
			if (null != vPCSResponse.socket) {
				if (!vPCSResponse.socket.isClosed()) {
					OutputStream outputStream = vPCSResponse.socket
							.getOutputStream();
					OutputStreamWriter outputStreamWriter = new OutputStreamWriter(
							outputStream, vPCSRequest.getRequestFileCode());
					vPCSResponse.bufferedWriter = new BufferedWriter(
							outputStreamWriter);
					int j = builderToString.length();
					vPCSResponse.bufferedWriter.flush();
					vPCSResponse.bufferedWriter.write(builderToString, 0, j);
					vPCSResponse.bufferedWriter.close();
				}
			}
			System.out.flush();
			S_logger.Log.logger.info("" + vPCSResponse.getHashCode() + "-f");
		} catch (Exception e) {
			System.out.flush();
			S_logger.Log.logger.info("" + e.getMessage());
			System.out.flush();
			S_logger.Log.logger.info("" + vPCSResponse.getHashCode()
					+ "-processBuffer bufferedWriter err");
			System.out.flush();
			S_logger.Log.logger.info("" + vPCSRequest.getRequestLink());
			System.out.flush();
			S_logger.Log.logger.info("" + vPCSRequest.getRequestFilePath());
			StableBlob.addException();
			return;
		}
	}
}